<template>
    <div class="big_box">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item>物资管理</el-breadcrumb-item>
            <el-breadcrumb-item>归还单</el-breadcrumb-item>
        </el-breadcrumb>
        <el-divider></el-divider>
        <el-form :model="selectfrom" ref="selectfrom_refs">
            <el-row :gutter="5">
                <el-col :span="3">
                    <el-input placeholder="归还单编号" v-model="selectfrom.ReturnOrder.ReturnOrderNumber"></el-input>
                </el-col>
                <el-col :span="3">
                    <el-input placeholder="物资名称" v-model="selectfrom.DeviceName"></el-input>
                </el-col>
                <el-col :span="3">
                    <el-input placeholder="物品编号" v-model="selectfrom.DeviceNumber"></el-input>
                </el-col>
                <el-col :span="3">
                    <el-select placeholder="请选择项目" filterable v-model="selectfrom.Project">
                        <el-option :value="0" label="请选择项目"></el-option>
                        <el-option v-for="v in AllProject" :label="v.ProjectName" :value="v.Id"></el-option>
                    </el-select>
                </el-col>
                <el-col :span="3">
                    <el-date-picker style="width: 100%" placeholder="开始日期"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    v-model="selectfrom.Start"></el-date-picker>
                </el-col>
                <el-col :span="3">
                    <el-date-picker style="width: 100%" placeholder="截止日期"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    v-model="selectfrom.End"></el-date-picker>
                </el-col>
                <el-col :span="3">
                    <el-select placeholder="归还单状态" v-model="selectfrom.ReturnOrder.State">
                        <el-option :value="0" label="单据状态"></el-option>
                        <el-option :value="1" label="待审批"></el-option>
                        <el-option :value="2" label="一级审批"></el-option>
                        <el-option :value="4" label="二级审批"></el-option>
                    </el-select>
                </el-col>
                <!--<el-col :span="2.5" style="line-height: 38px">
        <el-switch inactive-text="我未审核" v-model="selectfrom.NoChecked"></el-switch>
    </el-col>-->
                <el-col :span="1.5">
                    <el-checkbox style="width:100%" v-model="selectfrom.NoChecked" label="我未审核" border></el-checkbox>
                </el-col>
                <el-col :span="2">
                    <el-button icon="el-icon-search" @click="formdata"></el-button>
                </el-col>
            </el-row>
        </el-form>
        <el-row style="padding: 10px 0px">
            <el-col>
                <el-button-group>
                    <el-button class="buttongroup" type="danger" icon="el-icon-delete" @click="BatchDelete_event">批量删除</el-button>
                </el-button-group>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="24">
                <el-table :data="tableData" border stripe style="width: 100%"  @selection-change="SelectionChange_event">
                    <el-table-column type="selection" width="40" align="center"></el-table-column>
                    <el-table-column fixed type="index" label="序号" width="50" align="center" :index="indexMethod"></el-table-column>
                    <el-table-column fixed prop="ReturnOrderNumber" show-overflow-tooltip label="编号" width="120" align="center"></el-table-column>
                    <el-table-column fixed prop="OperatId" label="归还人" show-overflow-tooltip  align="center">
                        <template slot-scope="slot">
                            {{ GetUserName(slot.row.OperatId) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="OperatDate" show-overflow-tooltip label="归还日期" align="center">
                        <template slot-scope="slot">
                            {{ PurchaseDate_conversion(slot.row.OperatDate) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="State" label="状态" show-overflow-tooltip  align="center">
                        <template slot-scope="slot">
                            <div v-html="InitStateColum(slot.row)"></div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="Remark" label="备注" show-overflow-tooltip  align="center"></el-table-column>
                    <el-table-column prop="AuditUserId1" label="库管签批"  show-overflow-tooltip  align="center">
                        <template slot-scope="slot">
                            {{ GetUserName(slot.row.AuditUserId1) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="AuditTime1" show-overflow-tooltip label="库管签批日期" align="center">
                        <template slot-scope="slot">
                            {{ PurchaseDate_conversion(slot.row.AuditTime1) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="AuditUserId2" label="财务签批"  show-overflow-tooltip  align="center">
                        <template slot-scope="slot">
                            {{ GetUserName(slot.row.AuditUserId2) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="AuditTime2" show-overflow-tooltip label="财务签批日期" align="center">
                        <template slot-scope="slot">
                            {{ PurchaseDate_conversion(slot.row.AuditTime2) }}
                        </template>
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" width="230" align="center">
                        <template slot-scope="scope">
                            <el-button type="warning" size="mini" plain  @click="ReturnOrderTable_show_event(scope.row)">归还单</el-button>
                            <el-button type="success" size="mini" plain  @click="ReturnOrderEdit_show_event(scope.row)">编辑</el-button>
                            <el-button type="danger" size="mini"  plain  @click="BatchDeleteOne_event(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
            <el-col :span="24">
                <div class="block">
                    <el-pagination
                            @size-change="formdata"
                            @current-change="formdata"
                            :current-page.sync="pagination.currentPage"
                            :page-sizes="pagination.pagesizes"
                            :page-size.sync="pagination.pagesize"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total.sync="pagination.total">
                    </el-pagination>
                </div>
            </el-col>
        </el-row>

        <el-dialog :visible.sync="ReturnOrderbox.dialog" :title="'修改归还单---'+ReturnOrderbox.title" width="80%">
            <UpdateReturnOrder :table-data="ReturnOrderlist"></UpdateReturnOrder>
        </el-dialog>

        <el-dialog :visible.sync="ReturnOrderTable_.dialog" :fullscreen="true" :title="'归还单---'+ReturnOrderTable_.title" width="90%" :before-close="onClose">
            <ReturnOrderTable :return-orderdetails="ReturnOrderdetails" :audit-rule="auditRule"></ReturnOrderTable>
        </el-dialog>

    </div>
</template>

<script>
    import * as publicjs from "../../../../public/Scriput/Public";
    import UpdateReturnOrder from "./UpdateReturnOrder";
    import ReturnOrderTable from "./ReturnOrderTable";

    export default {
        name: "ReturnOrder",
        components: {
            'UpdateReturnOrder': UpdateReturnOrder,
            'ReturnOrderTable': ReturnOrderTable,
        },
        data() {
            return {
                //表格查询数据
                selectfrom: {
                    DeviceName: '',
                    DeviceNumber: '',
                    NoChecked: false,
                    Start: '',
                    End: '',
                    ReturnOrder: {
                        ReturnOrderNumber: '',
                        State: 0,
                    },
                    Project: 0,
                },
                //表格数据
                tableData: [],
                //分页数据绑定
                pagination: {
                    currentPage: 1,
                    pagesizes: [10, 20, 30, 40, 50],
                    pagesize: 10,
                    total: 100
                },
                //选中的数据
                SelectMore_lis: [],
                //弹框数据
                ReturnOrderbox: {
                    dialog: false,
                    title: '',
                },
                ReturnOrderTable_: {
                    dialog: false,
                    title: '',
                },
                //单个归还单的物资数据
                ReturnOrderlist: [],
                //单条归还单数据详情
                ReturnOrderdetails: {},
                // OA审批规则实体
                auditRule: {},
                //所有项目
                AllProject: [],
            }
        },
        created() {
            this.AllProject = publicjs.GetAllProject(this.$http)
            this.formdata()

        },
        methods: {
            //初始化表格数据加载
            formdata() {
                let loadding = this.openLoading();
                this.$http.post('Device/GetReturnOrder', {
                    currentPage: this.pagination.currentPage,
                    pagesize: this.pagination.pagesize,
                    DeviceName: this.selectfrom.DeviceName,
                    DeviceNumber: this.selectfrom.DeviceNumber,
                    NoChecked: this.selectfrom.NoChecked,
                    ReturnOrder: this.selectfrom.ReturnOrder,
                    Start: this.selectfrom.Start,
                    End: this.selectfrom.End,
                    Project: this.selectfrom.Project,
                }).then(res => {
                    loadding.close();
                    this.pagination.total = res.data.count
                    this.tableData = res.data.data
                }).catch(() => {
                    loadding.close();
                    this.$message.error('服务器异常,请联系管理员!')
                })
            },
            //批量删除
            BatchDelete_event() {
                let idlis = []
                let delect_bool = true
                if (this.SelectMore_lis.length == 0) {
                    this.$message.warning('您还未选择需要删除的设备!')
                } else {
                    for (let i = 0; i < this.SelectMore_lis.length; i++) {
                        idlis.push(this.SelectMore_lis[i].Id)
                        if (this.SelectMore_lis[i].Amount > this.SelectMore_lis[i].StockAmount) {
                            this.$message.warning(this.SelectMore_lis[i].DeviceName + '存在未归还的物资，不能直接删除!')
                            delect_bool = false
                            break;
                        }
                    }
                    if (delect_bool) {
                        this.$confirm('你确定要删除吗?', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'error'
                        }).then(() => {
                            let loadding = this.openLoading();
                            this.$http.post('Device/DeleteReturnOrderContent', idlis).then(res => {
                                loadding.close();
                                if (res.data.IsSuccess) {
                                    this.$message.success(res.data.Msg)
                                    this.formdata()
                                    this.SelectMore_lis = []
                                } else {
                                    this.$message.warning(res.data.Msg)
                                }
                            }).catch(() => {
                                loadding.close();
                                this.$message.error('服务器异常,请联系管理员!')
                            })
                        }).catch(() => {
                            this.$message.info('已取消!')
                            this.SelectMore_lis = []
                        })
                    }
                }
            },
            //单个数据删除
            BatchDeleteOne_event(obj) {
                this.SelectMore_lis.push(obj)
                this.BatchDelete_event()
            },
            //表格数据选择事件
            SelectionChange_event(value) {
                this.SelectMore_lis = value
            },
            //表格数据序列号
            indexMethod(index) {
                return ((this.pagination.currentPage - 1) * this.pagination.pagesize) + index + 1
            },
            //获取对应人员名字
            GetUserName(value) {
                let name = ''
                if (value != 0) {
                    for (let i = 0; i < this.$store.state.AllUsers.length; i++) {
                        if (this.$store.state.AllUsers[i].Id == value) {
                            name = this.$store.state.AllUsers[i].UserName
                            return name
                        }
                    }
                }
            },
            //创建时间转化
            PurchaseDate_conversion(val) {
                if (val != null && val != '') {
                    return (val.split(' '))[0]
                } else {
                    return ''
                }
            },
            //状态转化
            InitStateColum(obj) {
                let vhtml = publicjs.InitStateColum(obj)
                return vhtml
            },
            //编辑页面弹框事件
            ReturnOrderEdit_show_event(obj) {
                this.$http.get('Device/ValidateReturnOrderState?data=' + obj.ReturnOrderNumber).then(res => {
                    if (res.data.IsSuccess) {
                        this.$http.get('Device/UpdateReturnOrder?ReturnOrderNumber=' + obj.ReturnOrderNumber).then(res => {
                            if (res.data.IsSuccess) {
                                this.ReturnOrderlist = JSON.parse(res.data.Msg)
                                this.ReturnOrderbox.dialog = true
                                this.ReturnOrderbox.title = obj.ReturnOrderNumber
                            } else {
                                this.$message.warning(res.data.Msg)
                            }
                        })
                    } else {
                        this.$message.info(res.data.Msg)
                    }
                })
            },
            //归还单弹框
            ReturnOrderTable_show_event(val) {
                this.$http.get('Report/ReturnOrder?ReturnOrderNumber=' + val.ReturnOrderNumber).then(res => {
                    if (res.data.obj != null) {
                        this.ReturnOrderdetails = res.data.obj
                        this.auditRule = res.data.auditRule
                        this.ReturnOrderTable_.dialog = true
                        this.ReturnOrderTable_.title = val.ReturnOrderNumber
                        let reportMianShow = window.parent.document.getElementsByClassName("reportMianShow");
                        for (let i = 0; i < reportMianShow.length; i++) {
                            reportMianShow[i].style.display = "none";
                        }
                    }
                })
            },
            async onClose(done){
                await done();
                let reportMianShow = window.parent.document.getElementsByClassName("reportMianShow");
                for (let i = 0; i < reportMianShow.length; i++) {
                    reportMianShow[i].style.display = "inline-block";
                }
            },
        }
    }
</script>

<style scoped>

</style>
